package leetcode.秋招.美团;

import java.util.HashMap;
import java.util.Scanner;

/**
 * @author ：zsy
 * @date ：Created 2021/8/8 11:46
 * @description：
 */
public class Main2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int k = in.nextInt();
        HashMap<Integer, TreeNode> map = new HashMap<>();
        for (int i = 1; i <= n; i++) {
            map.put(i, new TreeNode(i));
        }
        for (int i = 1; i <= n; i++) {
            int left = in.nextInt();
            int right = in.nextInt();
            TreeNode node = map.get(i);
            if (left != 0)
                node.left = map.get(left);
            if (right != 0)
                node.right = map.get(right);
        }
        for (int i = 0; i < m; i++) {
            int j = in.nextInt();
            TreeNode treeNode = map.get(j);
            TreeNode tmp = treeNode.left;
            treeNode.left = treeNode.right;
            treeNode.right = tmp;
        }
        infixOrder(map.get(k));
    }
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int x) { val = x; }

        @Override
        public String toString() {
            return "TreeNode{" +
                    "val=" + val +
                    '}';
        }
    }

    public static void infixOrder(TreeNode node) {
        if (node == null) {
            return;
        }
        infixOrder(node.left);
        System.out.print(node.val + " ");
        infixOrder(node.right);
    }
}


